﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ICeFramework.Common.DataLayer
{
    public enum ValidationType
    { 
        Conversion = 0,
        Mandatory = 1,
        Length = 2
    }

    public static class ValidationHelper
    {
        public static string GetErrorMessage(ValidationType validationType, EntityColumn column)
        {
            switch (validationType)
            { 
                case ValidationType.Conversion:
                    return string.Format("The field {0} must be of type {1}", column.Name, column.ColumnType.ToString());
                case ValidationType.Length:
                    return string.Format("The field {0} length must be {1}", column.Name, column.Length.ToString());
                case ValidationType.Mandatory:
                    return string.Format("The field {0} is mandatory", column.Name);
                default:
                    return "Generic Error Message";
            }
        }

        public static bool ValidateValue(object value, Type columnType, bool isMandatory)
        {
            if (isMandatory && (value == null || value == DBNull.Value))
            {
                return false;
            }
            if (value == null || value == DBNull.Value)
            {
                return true;
            }
            try
            {
                switch (columnType.ToString())
                {
                    case "System.String":
                    case "System.Int16":
                    case "System.Int32":
                    case "System.Int64":
                    case "System.Single":
                    case "System.Double":
                    case "System.Decimal":
                        Convert.ChangeType(value, columnType);
                        return true;
                }
            }
            catch 
            {
            }
            return false;
        }
    }
}
